package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEditEngineeringInstallOrderView;
import com.cloudsigns.factorymanager2.engineering.IView.IEditPublicationOrderView;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.EditEngineeringInstallOrderPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EditPublicationOrderPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.Calendar;
import java.util.Locale;


/**
 * Created by tony on 2018/1/26.
 */

public class EditEngineeringInstallationOrderActivity extends Activity implements View.OnClickListener, IEditEngineeringInstallOrderView, IEditPublicationOrderView {
    private RelativeLayout rl_engineering;
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView btn_edit;
    private LinearLayout ll_startorder_date, ll_completionorder_date;
    private ImageView img_startorder_date,img_completionorder_date;
    private TextView tv_project_name, tv_engineering_name;
    private TextView edt_startorder_date, edt_completionorder_date;
    private OrderParentBean orderParentBean;
    private String TAG = EditEngineeringInstallationOrderActivity.class.getSimpleName();
    private EditEngineeringInstallOrderPresenter engineeringInstallOrderPresenter;
    private EditPublicationOrderPresenter editPublicationOrderPresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_edit_engineering_installation_order);


        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

        }

        initData();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_engineering = findViewById(R.id.rl_engineering);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        btn_edit = findViewById(R.id.btn_edit);
        ll_startorder_date = findViewById(R.id.ll_startorder_date);
        ll_completionorder_date = findViewById(R.id.ll_completionorder_date);
        edt_startorder_date = findViewById(R.id.edt_startorder_date);
        edt_completionorder_date = findViewById(R.id.edt_completionorder_date);
        img_startorder_date=findViewById(R.id.img_startorder_date);
        img_completionorder_date=findViewById(R.id.img_completionorder_date);

        rl_back.setOnClickListener(this);
        rl_engineering.setOnClickListener(this);
        btn_edit.setOnClickListener(this);
        img_startorder_date.setOnClickListener(this);
        img_completionorder_date.setOnClickListener(this);
    }

    private void initData() {
        if (null != orderParentBean) {
            // 工程安装
            if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.INSTALL)) {
                tv_title.setText(getResources().getString(R.string.engineering_installation));
                engineeringInstallOrderPresenter = new EditEngineeringInstallOrderPresenter(this);
            }
            // 工程施工
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.CONSTRUCT)) {
                tv_title.setText(getResources().getString(R.string.engineering_construct));

            }
            // 批量安装
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.BATCH_INSTALL)) {
                tv_title.setText(getResources().getString(R.string.batch_engineering));
            }
            // 维修
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.MAINTAIN)) {
                tv_title.setText(getResources().getString(R.string.maintenance_order));
            }
            // 广告上刊
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.PUBLICATION)) {
                tv_title.setText(getResources().getString(R.string.publication));
                editPublicationOrderPresenter = new EditPublicationOrderPresenter(this);
            }
            // 巡查
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.PATROL)) {
                tv_title.setText("巡查工单");
                editPublicationOrderPresenter = new EditPublicationOrderPresenter(this);

            }
            // 现场勘察
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.SURVEY)) {
                tv_title.setText(getResources().getString(R.string.engineering_patro));
                editPublicationOrderPresenter = new EditPublicationOrderPresenter(this);
            }

            tv_project_name.setText(orderParentBean.getProjectName());
            tv_engineering_name.setText("/" + orderParentBean.getEngineeringName());

            CommonEnum.EngineeringOrderStatus engineeringOrderStatus = orderParentBean.getStatus();
            if (engineeringOrderStatus== CommonEnum.EngineeringOrderStatus.CREATED) {

            } else if (engineeringOrderStatus== CommonEnum.EngineeringOrderStatus.EDITED) {


            } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.SEND) {

            } else if (engineeringOrderStatus== CommonEnum.EngineeringOrderStatus.RECEIVED) {

            } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.RETURNED) {

            } else if (engineeringOrderStatus== CommonEnum.EngineeringOrderStatus.COMPLETED) {

            } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.CONSTRUCT) {

            } else if (engineeringOrderStatus== CommonEnum.EngineeringOrderStatus.PAUSE) {

            }
        }


    }

    private void confirmEdit() {
        if (null != orderParentBean) {
            // 工程安装
            if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.INSTALL)) {
                engineeringInstallOrderPresenter.editEngineeringInstallOrder();
            }
            // 工程施工
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.CONSTRUCT)) {


            }
            // 批量安装
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.BATCH_INSTALL)) {

            }
            // 维修
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.MAINTAIN)) {

            }
            // 广告上刊
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.PUBLICATION)) {
                editPublicationOrderPresenter.editPublicationOrder();
            }
            // 巡查
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.PATROL)) {
                editPublicationOrderPresenter.editPublicationOrder();

            }
            // 现场勘察
            else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.SURVEY)) {
                editPublicationOrderPresenter.editPublicationOrder();

            }

        }

    }

    private void showStartDateDialog() {
        DateViewDialog startDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_startorder_date.setText(time);
                    }
                });
        startDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());


        startDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        startDateChooseDialog.showDateChooseDialog();
//        final Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());
//        dateAndTime.set(2018, 2, 9);
//        STDatePickerDialog pickerDialog = new STDatePickerDialog(this, new STDatePickerDialog.OnDateSetListener() {
//
//            @Override
//            public void onDateSet(STDatePicker view, int year, int monthOfYear, int dayOfMonth, String formatedDate) {
//
//
//                // 修改日历控件的年，月，日
//
//                // 这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致
//                if (DateUtil.compareDate(String.valueOf(year), String.valueOf(monthOfYear + 1), String.valueOf(dayOfMonth), view)) {
//
//                    dateAndTime.set(Calendar.YEAR, year);
//                    dateAndTime.set(Calendar.MONTH, monthOfYear);
//                    dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
//
//                    edt_startorder_date.setText(formatedDate);
//                } else {
//
////                    ToastManager.getInstance().show(context, R.string.choose_right_birthday);
//                }
//            }
//        }, dateAndTime.get(Calendar.YEAR), dateAndTime.get(Calendar.MONTH), dateAndTime.get(Calendar.DAY_OF_MONTH));
//        pickerDialog.show();
    }

    private void showDateDialog() {
        DateViewDialog completeDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completionorder_date.setText(time);
                    }
                });
        completeDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        completeDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        completeDateChooseDialog.showDateChooseDialog();

    }

    @Override
    public void onClick(View view) {

        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_engineering:
                if (null != orderParentBean) {
                    // 工程安装
                    if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.INSTALL)) {
                        mIntent = new Intent(EditEngineeringInstallationOrderActivity.this, ViewEngineeringInstallOrderDetailActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, orderParentBean);
                        startActivity(mIntent);
                    }
                    // 工程施工
                    else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.CONSTRUCT)) {


                    }
                    // 批量安装
                    else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.BATCH_INSTALL)) {

                    }
                    // 维修
                    else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.MAINTAIN)) {
                        mIntent = new Intent(EditEngineeringInstallationOrderActivity.this, ReceiveMaintenanceOrderActivity.class);
                        mIntent.putExtra("type","EditEngineeringInstallationOrderActivity");
                        startActivity(mIntent);
                    }
                    // 广告上刊
                    else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.PUBLICATION)) {
                       // mIntent = new Intent(EditEngineeringInstallationOrderActivity.this, PublicationOrderDetailActivity.class);
                        mIntent = new Intent(EditEngineeringInstallationOrderActivity.this, EngineeringPublicationOrderHasBeenReceivedActicity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, orderParentBean);
                        startActivity(mIntent);
                    }
                    // 巡查
                    else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.PATROL)) {


                    }
                    // 现场勘察
                    else if (orderParentBean.getCategory().equals(CommonEnum.EngineeringORderCategory.SURVEY)) {
                        mIntent = new Intent(EditEngineeringInstallationOrderActivity.this, EngineeringSurveyOrderHasBeenReceivedActicity.class);
                        startActivity(mIntent);
                    }

                }


//                mIntent = new Intent(EditEngineeringInstallationOrderActivity.this, EditEngineeringInstallationOrderDetailActivity.class);
//                startActivity(mIntent);
                break;
            case R.id.btn_edit:
                confirmEdit();
                break;
                // 开工日期
            case R.id.img_startorder_date:
                showStartDateDialog();
                break;
                // 完工日期
            case R.id.img_completionorder_date:
                showDateDialog();
                break;

        }


    }

    @Override
    public long getBeginDate() {
        String beginDate = edt_startorder_date.getText().toString().trim();
        long beginDatel = DateUtil.getMillis(beginDate) + (24 * 60 * 60 * 1000) - 1;

        return beginDatel;

    }

    @Override
    public long getFinishDate() {
        String comDate = edt_completionorder_date.getText().toString().trim();
        return DateUtil.getMillis(comDate);
    }

    @Override
    public String getId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public void editPublicationOrderSunccessed() {
        Toast.makeText(this, "编辑成功！", Toast.LENGTH_SHORT).show();
        setResult(6, new Intent());
        finish();
    }

    @Override
    public void editPublicationOrderFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void editEngineeringInstallOderSunccessed() {
        Toast.makeText(this, "编辑成功！", Toast.LENGTH_SHORT).show();
        setResult(6, new Intent());
        finish();
    }

    @Override
    public void editEngineeringInstallOderFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
